package com.lmf.annotation;

import com.lmf.annotation.bean.Person_2;
import com.lmf.annotation.config.MyConfigOfPropertyValue;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

public class IOCTest_BeanPropertValue {
    //测试 @Value()注解赋值
    @Test
    public void test_1(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfigOfPropertyValue.class);
        print(context);
        System.out.println("--------------------------");
        Person_2 person2 = context.getBean("com.lmf.annotation.bean.Person_2", Person_2.class);
        System.out.println(person2);
        System.out.println("--------------------------");
        //也可以从当前配置环境中，获取属性文件中的值
        ConfigurableEnvironment environment = context.getEnvironment();
        String property = environment.getProperty("person_2.work"); //属性文件中的值
        String property2 = environment.getProperty("OS"); //系统环境中的属性文件中的值
        System.out.println(property+"  ||  "+property2);
    }
    public void print(AnnotationConfigApplicationContext context){
        String[] strings = context.getBeanDefinitionNames();
        for (String string : strings) {
            System.out.println("注册类："+string);
        }
    }
}
